package com.ocom.webapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.request.payconfig.NewVoucherTypeRequest;
import com.ocom.common.request.payconfig.UpdateVoucherTypeRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.PayConfigClientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class CouponController {

    private final Logger log = LoggerFactory.getLogger(CouponController.class);

    @Autowired
    PayConfigClientService payConfigClientService;

    //消费券种类列表
    @PostMapping(WebApiServiceUrl.URL_VoucherTypeonShow)
    public Result voucherTypeOnShow(@RequestBody JSONObject jsonObject){
        return payConfigClientService.voucherTypeList(jsonObject.getLong("comId"),jsonObject.getString("attribute"),jsonObject.getString("consType"));
    }

    //增加消费券种类
    @PostMapping(WebApiServiceUrl.URL_VoucherTypeAdd)
    public Result voucherTypeAdd(@RequestBody NewVoucherTypeRequest newVoucherTypeRequest){
        YoCiUser yoCiUser= SecurityUtils.getYoCiUser();
        newVoucherTypeRequest.setUserId(yoCiUser.getId());
        return payConfigClientService.newVoucherType(newVoucherTypeRequest);
    }

    //修改消费券种类
    @PostMapping(WebApiServiceUrl.URL_VoucherTypeModify)
    public Result voucherTypeModify(@RequestBody UpdateVoucherTypeRequest updateVoucherTypeRequest){
        return payConfigClientService.updateVoucherType(updateVoucherTypeRequest);
    }

    //删除消费券种类
    @PostMapping(WebApiServiceUrl.URL_VoucherTypeDelete)
    public Result voucherTypeDelete(@RequestBody JSONObject jsonObject){
        return payConfigClientService.removeVoucherType(jsonObject.getLong("comId"),jsonObject.getLong("voucherTypeId"));
    }
}
